//
//  CommunSecViewController.m
//  WhereToTravel
//
//  Created by dllo on 15/10/15.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "CommunSecViewController.h"
#import "CommunityModel.h"
#import "ComTableViewCell.h"
#import "WebViewController.h"

@interface CommunSecViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
//page
{
    NSInteger page;
    NSMutableArray * _array;
}

@property(nonatomic,retain)NSMutableDictionary *dicData; //解析字典
@property(nonatomic,retain)UIImageView *imageViewGO;//图片

@property(nonatomic,retain)UITableView *tableView;


@property(nonatomic,retain)UILabel *labelName; //名字
@property(nonatomic,retain)UILabel *labelZhuTi; //主题
@property(nonatomic,retain)UIView *viewLine; //分割线

@end

@implementation CommunSecViewController

// 视图的出现将tabbar显示,消失就隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];

    
    self.view.backgroundColor = [UIColor whiteColor];
    //tableview
    [self createTable];

    //请求数据
    [self createDATA];
      _array=[[NSMutableArray alloc]init];
    
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];

    // Do any additional setup after loading the view.
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createDATA {
    page++;
    NSString*url=[NSString stringWithFormat:@"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=%@&lat=38.88262860611146&lon=121.5396766802357&page=%ld&track_app_channel=App%%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&type=new&v=1%%20",self.UId,page];
    
    [ZSMAFNetWork geturl:url body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        
            self.dicData = [NSMutableDictionary dictionary];
            self.dicData = result[@"data"];
            for (NSMutableDictionary *dicLOL in self.dicData[@"entry"]) {
                CommunityModel *model = [[CommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:dicLOL];
                [_array addObject:model];
            }
        [self createGO];
        [_tableView reloadData];
        self.hud.hidden = YES;
        
    } failure:^(NSError *error) {
        
    }];
}

//标题 照片 主题
-(void)createGO
{
    //照片
    self.imageViewGO = [[UIImageView alloc]initWithFrame:CGRectMake(20 * WIDTH, 10 * HEIGHT, 60 * WIDTH, 60 * HEIGHT)];
    [self.view addSubview:self.imageViewGO];
    [self.imageViewGO sd_setImageWithURL:[NSURL URLWithString:self.dicData[@"photo"]]placeholderImage:[UIImage imageNamed:@"a682437ce8d3c26faeb3d53d04089507"]];
//    NSLog(@"+++++++%@",self.dicData[@"photo"]);
    //题目
    self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(90 * WIDTH, 10 * HEIGHT, 150 * WIDTH, 35 * HEIGHT)];
    [self.view addSubview:self.labelName];
    self.labelName.text = self.dicData[@"name"];
    self.labelName.numberOfLines = 0;
 
    //主题
    self.labelZhuTi = [[UILabel alloc]initWithFrame:CGRectMake(90 * WIDTH, 50 * HEIGHT, 80 * WIDTH, 20 * HEIGHT)];
    [self.view addSubview:self.labelZhuTi];
    NSString *strBad = [NSString stringWithFormat:@"%@个主题",self.dicData[@"total"]];
    self.labelZhuTi.font = [UIFont systemFontOfSize:12];
    self.labelZhuTi.text = strBad;

    [self.imageViewGO mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20 * WIDTH);
        make.top.equalTo(self.view.mas_top).offset(10 * HEIGHT);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.16);
        make.height.equalTo(@(60 * HEIGHT));
    }];
    [self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageViewGO.mas_right).offset(10 * WIDTH);
        make.top.equalTo(self.view.mas_top).offset(10 * HEIGHT);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.4);
        make.height.equalTo(@(35 * HEIGHT));
    }];
    [self.labelZhuTi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageViewGO.mas_right).offset(10 * WIDTH);
        make.top.equalTo(self.view.mas_top).offset(50 * HEIGHT);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.22);
        make.height.equalTo(@(20 * HEIGHT));
    }];
}
//tableView
-(void)createTable
{
    //分割线
    self.viewLine = [[UIView alloc]initWithFrame:CGRectMake(0, 85 * HEIGHT, 375 * WIDTH, 1 * HEIGHT)];
    [self.view addSubview:self.viewLine];
    self.viewLine.backgroundColor = [UIColor grayColor];
    self.viewLine.alpha = 0.5;
    [self.viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(85 * HEIGHT);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(1 * HEIGHT));
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 90 * HEIGHT, 375 * WIDTH, HEIGHT * 464)style:UITableViewStylePlain];
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.hud hide:YES afterDelay:2];
    self.tableView.dataSource = self;
    self.tableView.delegate =self;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 100 * HEIGHT;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.viewLine.mas_bottom);
    }];
}
// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}

//点击方法进入webView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController *webVC = [[WebViewController alloc]init];
    [self.navigationController pushViewController:webVC animated:YES];
    CommunityModel *model = _array[indexPath.row];
    webVC.url = model.view_url;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reause =@"reause";
    ComTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reause];
    if (!cell) {
        cell = [[ComTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reause];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    if (_array.count>indexPath.row) {
        //内容
        NSLog(@"*****%ld",_array.count);
        CommunityModel  *model = _array[indexPath.row];
        //标题
        cell.labelTitle.text = model.title;
        //字体
        cell.labelTitle.font = [UIFont systemFontOfSize:14];
        //行
        cell.labelTitle.numberOfLines = 0;
        //观看与评论,拼接
        NSString *strRP = [NSString stringWithFormat:@"👀  %@     R  %@",model.replys,model.views];
        cell.labelReplys.text = strRP;
        //字体
        cell.labelReplys.font = [UIFont systemFontOfSize:8];
        //日期
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[model.lastpost doubleValue]];
        //拼接时间
        NSString *nameStr =  [NSString stringWithFormat:@"%@",date];
        //截取
        NSString*str=[nameStr substringToIndex:19];
        //拼接时间与发帖人
        NSString*UserStr=[NSString stringWithFormat:@"%@ | %@",model.username,str];
        cell.labelUse.text=UserStr;
        //调字体
        cell.labelUse.font=[UIFont systemFontOfSize:10];
        
    }
    return cell;
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.viewLine.backgroundColor = [UIColor cyanColor];
        self.labelName.textColor = [UIColor whiteColor];
        self.labelZhuTi.textColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.viewLine.backgroundColor = [UIColor grayColor];
        self.labelName.textColor = [UIColor blackColor];
        self.labelZhuTi.textColor = [UIColor blackColor];
        self.view.backgroundColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
